//
//  NSString+Extension.m
//  TPSLIVE
//
//  Created by 唐佳霖 on 2018/1/10.
//  Copyright © 2018年 HS. All rights reserved.
//

#import "NSString+TPSExtension.h"

@implementation NSString (TPSExtension)

- (NSURL *)formatUrlString {
    BOOL ret = [self hasChinese];
    if (ret) {
        NSString *formatString = [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        return [NSURL URLWithString:formatString];
    }
    return [NSURL URLWithString:self];
}

- (NSString *)formatString {
    BOOL ret = [self hasChinese];
    if (ret) {
        NSString *formatString = [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        return formatString;
    }
    return self;
}

- (BOOL)hasChinese {
    for(int i=0; i< [self length];i++){
        int a = [self characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff)
        {
            return YES;
        }
    }
    return NO;
}


- (BOOL)containsHasSpecialString {
    //提示标签不能输入特殊字符
    NSString *str =@"^[\u4E00-\u9FA5A-Za-z0-9_]+$";
    NSPredicate* emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    if (![emailTest evaluateWithObject:self]) {
        return YES;
    }
    return NO;
}

- (BOOL)containsHasEmoji {
    NSString *pattern = @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:self];
    return isMatch;
}

- (NSInteger)getMixStringLength
{
    __block NSInteger stringLength = 0;
    
    [self enumerateSubstringsInRange:NSMakeRange(0, [self length])
                               options:NSStringEnumerationByComposedCharacterSequences
                            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
     {
         const unichar hs = [substring characterAtIndex:0];
         if (0xd800 <= hs && hs <= 0xdbff)
         {
             if (substring.length > 1)
             {
                 const unichar ls = [substring characterAtIndex:1];
                 const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                 if (0x1d000 <= uc && uc <= 0x1f77f)
                 {
                     stringLength += 1;
                 }
                 else
                 {
                     stringLength += 1;
                 }
             }
             else
             {
                 stringLength += 1;
             }
         } else if (substring.length > 1)
         {
             const unichar ls = [substring characterAtIndex:1];
             if (ls == 0x20e3)
             {
                 stringLength += 1;
             }
             else
             {
                 stringLength += 1;
             }
         } else {
             if (0x2100 <= hs && hs <= 0x27ff)
             {
                 stringLength += 1;
             }
             else if (0x2B05 <= hs && hs <= 0x2b07)
             {
                 stringLength += 1;
             }
             else if (0x2934 <= hs && hs <= 0x2935)
             {
                 stringLength += 1;
             }
             else if (0x3297 <= hs && hs <= 0x3299)
             {
                 stringLength += 1;
             }
             else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50)
             {
                 stringLength += 1;
             }
             else
             {
                 stringLength += 1;
             }
         }
     }];
    return stringLength;
}

- (NSString *)getThinImageUrlString {
    NSRange range = [self rangeOfString:@"?"];
    if (range.location == NSNotFound) {
        return [NSString stringWithFormat:@"%@?imageslim", self];
    } else {
        return [NSString stringWithFormat:@"%@&imageslim", self];
    }
}

- (NSURL *)filterChineseAndThin {
    NSString *tempString = [self getThinImageUrlString];
    return [tempString formatUrlString];
}

- (NSURL *)getFitAvatarUrl {
    NSString *tempString = [self getFitAvatarString];
    return [tempString formatUrlString];
}

- (NSString *)getFitAvatarString {
    NSRange range = [self rangeOfString:@"?"];
    if (range.location == NSNotFound) {
        return [NSString stringWithFormat:@"%@?w/50/h/50", self];
    } else {
        return [NSString stringWithFormat:@"%@/w/50/h/50", self];
    }
}

//- (NSString *)getNewDataStringWithFormat:(NSString *)formatString {
//    NSDate *starDate = [NSDate dateWithString:self format:@"yyyy-MM-dd HH:mm:ss"];
//    NSString *starstr = [self formattedStringWithFormat:formatString with:starDate];
//    return starstr;
//}

- (NSString *)formattedStringWithFormat:(NSString *)format with:(NSDate *)date {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    NSString *ret = [formatter stringFromDate:date];
    return ret;
}

- (NSString *)base64DecodedString {
    NSData *data = [[NSData alloc] initWithBase64EncodedString:self options:0];
    return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}

- (NSString *)base64EncodedString {
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    return [data base64EncodedStringWithOptions:0];
}
@end
